﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Word_Mojo
{
    /// <summary>
    /// This class provides static methods to the Form for quickly retrieving Tile 
    /// objects needed for different purposes.  The class can provide an "EmptyTile", 
    /// a "PlayerTile", or a "BoardTile".  Each type being used different in the 
    /// game.  
    /// </summary>
    class TileFactory
    {
        #region Private Data Members

        private static Dictionary<string, int> letterValues;

        #endregion

        #region Construction

        /// <summary>
        /// Static contructor for TileFactory that initializes and populates the hash 
        /// table containing the letter/point value associations.  
        /// </summary>
        static TileFactory()
        {
            letterValues = new Dictionary<string, int>(26);
            PopulateDictionary();
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Returns a new letter-initialized Tile object to be used by the player 
        /// directly on the board for spelling objects.  These Tile objects only 
        /// have a value and letter property - but no Point coordinate as they're not 
        /// placed on the board (yet).  
        /// </summary>
        /// <param name="stringLetter">The letter of the Tile needed</param>
        /// <returns>A new Tile object to be used for forming words</returns>
        public static Tile GetPlayerTile(string stringLetter)
        {
            return new Tile(letterValues[stringLetter], stringLetter);
        }

        /// <summary>
        /// Returns a new empty "blank" Tile object that the game board is 
        /// initially populated with.  These tiles have no cooresponding value 
        /// or letter, but do have a Point coordinate
        /// </summary>
        /// <param name="column">The column on the board where this Tile is used</param>
        /// <param name="row">The row on the board where this Tile is used</param>
        /// <returns>A new Tile object to be displayed on the game board</returns>
        public static Tile GetEmptyTile(int column, int row)
        {
            return new Tile(column, row);
        }

        /// <summary>
        /// Returns a new letter-initialized Tile object to be placed on the game 
        /// board.  The tile has a point, letter, and position property associated
        /// </summary>
        /// <param name="stringLetter">The letter of the Tile needed</param>
        /// <param name="column">The column on the board where this Tile is used</param>
        /// <param name="row">The row on the board where this Tile is used</param>
        /// <returns>A new Tile object to be used on the game board</returns>
        public static Tile GetBoardTile(string stringLetter, int column, int row)
        {
            return new Tile(letterValues[stringLetter], stringLetter, column, row);
        }

        #endregion

        #region Private Helper Methods

        /// <summary>
        /// Populates the hash table to contain the 26 letters and the point value 
        /// associated with each letter.  More commonly-used letters (e.g. "E", "A") 
        /// are assigned lower points than less-commonly-used letters (e.g. "Q", "Z")
        /// </summary>
        static void PopulateDictionary()
        {
            letterValues.Add("a", 1);
            letterValues.Add("b", 3);
            letterValues.Add("c", 3);
            letterValues.Add("d", 2);
            letterValues.Add("e", 1);
            letterValues.Add("f", 4);
            letterValues.Add("g", 2);
            letterValues.Add("h", 4);
            letterValues.Add("i", 1);
            letterValues.Add("j", 8);
            letterValues.Add("k", 5);
            letterValues.Add("l", 1);
            letterValues.Add("m", 3);
            letterValues.Add("n", 1);
            letterValues.Add("o", 1);
            letterValues.Add("p", 3);
            letterValues.Add("q", 10);
            letterValues.Add("r", 1);
            letterValues.Add("s", 1);
            letterValues.Add("t", 1);
            letterValues.Add("u", 1);
            letterValues.Add("v", 4);
            letterValues.Add("w", 4);
            letterValues.Add("x", 8);
            letterValues.Add("y", 4);
            letterValues.Add("z", 10);
        }

        #endregion
    }
}
